;
(function() {
	var versionInfo;
	module.exports = function(host) {
		var appConfigBase = {};
		var _isNewDev = host.indexOf("dev-dotnar.com") !== -1;
		var _isDev = host.indexOf("dev.dotnar.com") === 0 || _isNewDev;
		appConfigBase._isNewDev = _isNewDev;
		appConfigBase.isDev = _isDev;
		//前端版本号
		appConfigBase.version = versionInfo || module.exports.getVersion();;
		// console.log(appConfigBase.version);
		var server_url = appConfigBase.server_url = _isDev ? (_isNewDev ? "http://api.dev-dotnar.com/" : "http://dev.dotnar.com:3000/") : "http://api.dotnar.com/";
		appConfigBase.img_server_url = server_config_data.img_server_url;
		appConfigBase.short_url = _isDev ? "http://dev.dotnar.com:3003/" : "http://s.dotnar.com/";

		var wx = appConfigBase.wx = {};
		wx.access_token = server_url + "wx/access_token";
		wx.jsapi_ticket = server_url + "wx/jsapi_ticket";
		wx.jsapi_signature = server_url + "wx/jsapi_signature";

		appConfigBase.server_config = server_config_data;
		//host所对应的站点的版本号
		var version_name = host_version_maps[host] || "o2o_version";
		appConfigBase.host_version = versionInfo[version_name];
		return appConfigBase;
	};
	var exec_strs = {
		"*": "~/update_fontend_dev.sh & ~/update_lib.sh & ~/update_dotnar_admin.sh & ~/update_dotnar.sh & ~/update_dotnar_pay.sh",
		"Gaubee_O2O_front_end_lib": "~/update_lib.sh",
		"Gaubee_dotnarAdmin": "~/update_dotnar_admin.sh",
		"Gaubee_dotnar": "~/update_dotnar.sh",
		"Gaubee_dotnarPay": "~/update_dotnar_pay.sh",
		"xuezi_O2O_fontend": "~/update_fontend_xuezi.sh",
	};
	var version_maps = {
		"xuezi_O2O_fontend": "o2o_version",
		"Gaubee_O2O_front_end_lib": "lib_version",
		"Gaubee_dotnarAdmin": "bus_version",
		"Gaubee_dotnar": "www_version",
		"Gaubee_dotnarPay": "pay_version",
	};
	var host_version_maps = {
		"lib.dotnar.com": "lib_version",
		"www.dotnar.com": "www_version",
		"dotnarpay.dotnar.com": "pay_dotnar",
		"admin.dotnar.com": "bus_version",

		"dev.dotnar.com:2221": "lib_version",
		"dev.dotnar.com:2223": "www_version",
		"dev.dotnar.com:2224": "pay_dotnar",
		"dev.dotnar.com:2225": "bus_version",

	};
	module.exports.updateVersion = function(name, ver) {

		if (arguments.length === 1) {
			ver = name;
			name = "*";
		}
		console.log(name);
		if (!exec_strs[name]) { //name值非法，直接取消更新
			console.log("标识错误，更新取消");
			return;
		}
		var version_name = version_maps[name];

		//更新代码
		child_process.exec(exec_strs[name], function(error, stdout, stderr) {
			if (error !== null) {
				console.log('stderr: ' + stderr);
				console.log('获取最新前端代码出错: ' + error);
			}
			console.log('stdout: ' + stdout);
			console.log("获取最新前端代码完成");
		});
		versionInfo || (versionInfo = {});
		//外部强制更新版本号
		if (name == "*") { //更新所有仓库的版本号
			[
				"xuezi_O2O_fontend",
				"Gaubee_O2O_front_end_lib",
				"Gaubee_dotnarAdmin",
				"Gaubee_dotnar",
				"Gaubee_dotnarPay",
			].forEach(function(name) {
				try {
					var hook_info = fs.readFileSync(__dirname + "/." + name + ".hook.json").toString();
					hook_info = JSON.parse(hook_info);
				} catch (e) {
					hook_info = {
						push_data: {}
					};
				}
				hook_info.push_data.after = ver + name;
				fs.writeFileSync(__dirname + "/." + name + ".hook.json", JSON.stringify(hook_info, null, 4));
				var version_name = version_maps[name];
				versionInfo[version_name] = ver + name;
			});
		} else { //更新指定仓库的版本号信息
			versionInfo[version_name] = ver;
		}
		return versionInfo;

	};
	module.exports.getVersion = function() { //从文件中读取各个仓库的版本号信息
		versionInfo = {};
		[
			"xuezi_O2O_fontend",
			"Gaubee_O2O_front_end_lib",
			"Gaubee_dotnarAdmin",
			"Gaubee_dotnar",
			"Gaubee_dotnarPay",
		].forEach(function(name) {
			try {
				var hook_info = fs.readFileSync(__dirname + "/." + name + ".hook.json").toString();
				hook_info = JSON.parse(hook_info);
				var version = hook_info.push_data.after;
			} catch (e) {}
			version || (version = (+new Date) + name)
			var version_name = version_maps[name];
			versionInfo[version_name] = version;
			console.log(name, version_name, version)
		});
		console.log("当前版本信息:", versionInfo);
		return versionInfo;
	}

	var fs = require("fs");
	var child_process = require("child_process");
	var config = require("../../../core/_include/config");
	var server_config_data = {};
	Object.keys(config).forEach(function(key) {
		if (typeof config[key] !== "object") { //复合对象不要参与进来
			server_config_data[key] = config[key];
		}
	});
}());